import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**试练塔buff属性枚举*/
export const enum E_TrialTowerBuffRefrsh {
	/**免费使用次数*/
	FREE_REFRESH_USE = 0,
	/**广告使用次数*/
	AD_REFRESH_USE = 1,
	/**己方的buff id*/
	SELF_BUFF = 2,
	/**敌方的buff id*/
	ENEMY_BUFF = 3,
	/**上次刷新时间*/
	REFRESH_TIME = 4,
	/**枚举数量*/
	EnumCount = 5
}


/**试练塔buff*/
export class TrialTowerBuffRefrsh {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_TRIAL_TOWER_BUFF_REFRSH_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_TRIAL_TOWER_BUFF_REFRSH_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: TrialTowerBuffRefrsh[] = [];

	/**创建入口*/
	public static create(json?: string): TrialTowerBuffRefrsh {
		const c = this.POOL.pop() || new TrialTowerBuffRefrsh();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**免费使用次数 (uint16)*/
	protected _freeRefreshUse: number;
	/**广告使用次数 (uint16)*/
	protected _adRefreshUse: number;
	/**己方的buff id (uint32)*/
	protected _selfBuff: number;
	/**敌方的buff id (uint32)*/
	protected _enemyBuff: number;
	/**上次刷新时间 (long)*/
	protected _refreshTime: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (TrialTowerBuffRefrsh.POOL.indexOf(this) === -1) {
			TrialTowerBuffRefrsh.POOL.push(this);
		}
	}

	/**免费使用次数*/
	public get freeRefreshUse() {
		return this._freeRefreshUse;
	}


	/**免费使用次数*/
	public set freeRefreshUse(v: number) {
		this._freeRefreshUse = v;
	}


	/**广告使用次数*/
	public get adRefreshUse() {
		return this._adRefreshUse;
	}


	/**广告使用次数*/
	public set adRefreshUse(v: number) {
		this._adRefreshUse = v;
	}


	/**己方的buff id*/
	public get selfBuff() {
		return this._selfBuff;
	}


	/**己方的buff id*/
	public set selfBuff(v: number) {
		this._selfBuff = v;
	}


	/**敌方的buff id*/
	public get enemyBuff() {
		return this._enemyBuff;
	}


	/**敌方的buff id*/
	public set enemyBuff(v: number) {
		this._enemyBuff = v;
	}


	/**上次刷新时间*/
	public get refreshTime() {
		return this._refreshTime;
	}


	/**上次刷新时间*/
	public set refreshTime(v: number) {
		this._refreshTime = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_TrialTowerBuffRefrsh): any {
		switch (k) {
			case E_TrialTowerBuffRefrsh.FREE_REFRESH_USE: return this._freeRefreshUse;
			case E_TrialTowerBuffRefrsh.AD_REFRESH_USE: return this._adRefreshUse;
			case E_TrialTowerBuffRefrsh.SELF_BUFF: return this._selfBuff;
			case E_TrialTowerBuffRefrsh.ENEMY_BUFF: return this._enemyBuff;
			case E_TrialTowerBuffRefrsh.REFRESH_TIME: return this._refreshTime;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_TrialTowerBuffRefrsh, v: any): void {
		switch (k) {
			case E_TrialTowerBuffRefrsh.FREE_REFRESH_USE: this._freeRefreshUse = v; break;
			case E_TrialTowerBuffRefrsh.AD_REFRESH_USE: this._adRefreshUse = v; break;
			case E_TrialTowerBuffRefrsh.SELF_BUFF: this._selfBuff = v; break;
			case E_TrialTowerBuffRefrsh.ENEMY_BUFF: this._enemyBuff = v; break;
			case E_TrialTowerBuffRefrsh.REFRESH_TIME: this._refreshTime = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//免费使用次数
		this._freeRefreshUse = 0;
		//广告使用次数
		this._adRefreshUse = 0;
		//己方的buff id
		this._selfBuff = 0;
		//敌方的buff id
		this._enemyBuff = 0;
		//上次刷新时间
		this._refreshTime = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._freeRefreshUse) {
			json.freeRefreshUse = this._freeRefreshUse;
		}
		if (this._adRefreshUse) {
			json.adRefreshUse = this._adRefreshUse;
		}
		if (this._selfBuff) {
			json.selfBuff = this._selfBuff;
		}
		if (this._enemyBuff) {
			json.enemyBuff = this._enemyBuff;
		}
		if (this._refreshTime) {
			json.refreshTime = this._refreshTime;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.freeRefreshUse) {
			this._freeRefreshUse = json.freeRefreshUse;
		}
		if (json.adRefreshUse) {
			this._adRefreshUse = json.adRefreshUse;
		}
		if (json.selfBuff) {
			this._selfBuff = json.selfBuff;
		}
		if (json.enemyBuff) {
			this._enemyBuff = json.enemyBuff;
		}
		if (json.refreshTime) {
			this._refreshTime = json.refreshTime;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint16(this._freeRefreshUse);
		byte.writeUint16(this._adRefreshUse);
		byte.writeUint32(this._selfBuff);
		byte.writeUint32(this._enemyBuff);
		byte.writeLongNumber(this._refreshTime);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._freeRefreshUse = byte.readUint16();
		this._adRefreshUse = byte.readUint16();
		this._selfBuff = byte.readUint32();
		this._enemyBuff = byte.readUint32();
		this._refreshTime = byte.readLongNumber();
		return this;
	}


	/**克隆*/
	public clone(): TrialTowerBuffRefrsh {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = TrialTowerBuffRefrsh.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = TrialTowerBuffRefrsh.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//免费使用次数
			if (idx === E_TrialTowerBuffRefrsh.FREE_REFRESH_USE) {
				const oldv = this._freeRefreshUse;
				this._freeRefreshUse = us[i + 1];
				IAsync.dispatch(eventID, idx, this._freeRefreshUse, oldv);
				i += 2;
				continue;
			}
			//广告使用次数
			if (idx === E_TrialTowerBuffRefrsh.AD_REFRESH_USE) {
				const oldv = this._adRefreshUse;
				this._adRefreshUse = us[i + 1];
				IAsync.dispatch(eventID, idx, this._adRefreshUse, oldv);
				i += 2;
				continue;
			}
			//己方的buff id
			if (idx === E_TrialTowerBuffRefrsh.SELF_BUFF) {
				const oldv = this._selfBuff;
				this._selfBuff = us[i + 1];
				IAsync.dispatch(eventID, idx, this._selfBuff, oldv);
				i += 2;
				continue;
			}
			//敌方的buff id
			if (idx === E_TrialTowerBuffRefrsh.ENEMY_BUFF) {
				const oldv = this._enemyBuff;
				this._enemyBuff = us[i + 1];
				IAsync.dispatch(eventID, idx, this._enemyBuff, oldv);
				i += 2;
				continue;
			}
			//上次刷新时间
			if (idx === E_TrialTowerBuffRefrsh.REFRESH_TIME) {
				const oldv = this._refreshTime;
				this._refreshTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._refreshTime, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in TrialTowerBuffRefrsh with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: TrialTowerBuffRefrsh): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._freeRefreshUse !== other._freeRefreshUse) {
			updates.push(E_TrialTowerBuffRefrsh.FREE_REFRESH_USE, this._freeRefreshUse);
		}
		if (this._adRefreshUse !== other._adRefreshUse) {
			updates.push(E_TrialTowerBuffRefrsh.AD_REFRESH_USE, this._adRefreshUse);
		}
		if (this._selfBuff !== other._selfBuff) {
			updates.push(E_TrialTowerBuffRefrsh.SELF_BUFF, this._selfBuff);
		}
		if (this._enemyBuff !== other._enemyBuff) {
			updates.push(E_TrialTowerBuffRefrsh.ENEMY_BUFF, this._enemyBuff);
		}
		if (this._refreshTime !== other._refreshTime) {
			updates.push(E_TrialTowerBuffRefrsh.REFRESH_TIME, this._refreshTime);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(TrialTowerBuffRefrsh.TOTAL_EVENT_ID, this.getV(0));
	}

}
